package com.yxjx.duoxue.payment.datamodel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;

import com.yxjx.duoxue.course.CourseOrderKidsOption;
import com.yxjx.duoxue.datamodel.DMCourseCategory;
import com.yxjx.duoxue.global.Globals;
import com.yxjx.duoxue.payment.ClientUserKidsDTS;
import com.yxjx.duoxue.payment.ConfigKidsDetailDTS;
import com.yxjx.duoxue.payment.OrderKidsConfigurationView;

/**
 * 在KidInfoEditActivity和OrderSubmitActivity间传递。
 * @author mengmengchen
 *
 */
public class DMKidInfoRefObject implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2052373552368669464L;
	private String childName;
	private Integer childSex;
	private Integer KidBirthYear;
	private Integer KidBIrthMonth;
	private ArrayList<DMCourseCategory> selectedCourseTypes;
	private ArrayList<ConfigKidsDetailDTS> selectedLabels;
	
	public String getChildName() {
		return childName;
	}
	public void setChildName(String childName) {
		this.childName = childName;
	}
	public Integer getChildSex() {
		return childSex;
	}
	public void setChildSex(Integer childSex) {
		this.childSex = childSex;
	}
	public Integer getKidBirthYear() {
		return KidBirthYear;
	}
	public void setKidBirthYear(Integer kidBirthYear) {
		KidBirthYear = kidBirthYear;
	}
	public Integer getKidBIrthMonth() {
		return KidBIrthMonth;
	}
	public void setKidBIrthMonth(Integer kidBIrthMonth) {
		KidBIrthMonth = kidBIrthMonth;
	}
	public ArrayList<DMCourseCategory> getSelectedCourseTypes() {
		return selectedCourseTypes;
	}
	public void setSelectedCourseTypes(
			ArrayList<DMCourseCategory> selectedCourseTypes) {
		this.selectedCourseTypes = selectedCourseTypes;
	}
	public ArrayList<ConfigKidsDetailDTS> getSelectedLabels() {
		return selectedLabels;
	}
	public void setSelectedLabels(ArrayList<ConfigKidsDetailDTS> selectedLabels) {
		this.selectedLabels = selectedLabels;
	}
	
	public static DMKidInfoRefObject from(Context ctx, ClientUserKidsDTS item) {
		if (item == null) {
			return null;
		}
		
		DMKidInfoRefObject refObj = new DMKidInfoRefObject();
		refObj.setChildName(item.getKidsName());
		refObj.setChildSex(item.getKidsSex());
		refObj.setKidBIrthMonth(item.getKidsBirthMonth());
		refObj.setKidBirthYear(item.getKidsBirthYear());
		// TODO 将ClientUserKidsDTS转换为DMKidInfoRefObject，再传递给KidInfoEditActiivty.
		CourseOrderKidsOption kidOption = Globals.getInstance(ctx).getKidOption();
		if (kidOption != null && kidOption.getKidsConfigList() != null && kidOption.getKidsConfigList().size() != 0) {
			refObj.setSelectedLabels(getSelectedLabels(kidOption.getKidsConfigList(), item.getKidsDetail()));
		}
		ArrayList<ArrayList<DMCourseCategory>> types = Globals.getInstance(ctx).getCourseTpes();
		if (types != null && types.size() > 0) {
			refObj.setSelectedCourseTypes(getSelectedCourseTypesInternal(types, item.getCourseType()));
		}
		return refObj;
	}
	
	private static ArrayList<DMCourseCategory> getSelectedCourseTypesInternal(
			ArrayList<ArrayList<DMCourseCategory>> types, String string) {
		ArrayList<Integer> ids = OrderKidsConfigurationView.str2IntegerArray(string);
		HashMap<Integer, DMCourseCategory> idTypeMap = new HashMap<Integer, DMCourseCategory>();
		for (ArrayList<DMCourseCategory> row : types) {
			for (DMCourseCategory type : row) {
				idTypeMap.put(type.getType(), type);
			}
		}
		
		ArrayList<DMCourseCategory> ret = new ArrayList<DMCourseCategory>();
		for (Integer id : ids) {
			ret.add(idTypeMap.get(id));
		}
		return ret;
	}
	private static ArrayList<ConfigKidsDetailDTS> getSelectedLabels(
			List<ConfigKidsDetailDTS> kidsConfigList, String string) {
		ArrayList<Integer> ids = OrderKidsConfigurationView.str2IntegerArray(string);
		HashMap<Integer, ConfigKidsDetailDTS> idTypeMap = new HashMap<Integer, ConfigKidsDetailDTS>();
		for (ConfigKidsDetailDTS type : kidsConfigList) {
			idTypeMap.put(type.getId(), type);
		}
		
		ArrayList<ConfigKidsDetailDTS> ret = new ArrayList<ConfigKidsDetailDTS>();
		for (Integer id : ids) {
			ret.add(idTypeMap.get(id));
		}
		return ret;
	}
	ArrayList<DMCourseCategory> from(Context context, String typeIdString) {
		ArrayList<DMCourseCategory> types = new ArrayList<DMCourseCategory>();
		return types;
	}
	
}
